$(function () {
    //全局定义页数与总条数
    var markid = getUrlString('markid');
    var key = getUrlString('key');
    // var markid = '3b70887e5d841ff8ab7388ea5f41a5af';
    var page = -1,totalSize = 0,sort= 'time',sptypes = '',maintypes = '',maxmoney = '999999',minmoney = '1',datatype='',filter = 1;
    $(document).on('click', '.all', function() {
        var buttons1 = [
            {
                text: '全部',
                bold: true,
                onClick: function () {
                    $('.option').removeClass('check');
                    $('#screenImg').attr('src','../../res/img/common/sx.png');
                    $('.screen').removeClass('common');
                    $(".all").html('全部<img src="../../res/img/common/downJt.png" />');
                    $('#usedList').empty();//清空容器
                    page = 0;
                    totalSize = 0;
                    minmoney = '1';
                    maxmoney = '999999';
                    sptypes = "";
                    maintypes = '';
                    loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,datatype,key)
                }
            },
            {
                text: '现货',
                bold: true,
                onClick: function () {
                    $('.option').removeClass('check');
                    $('#screenImg').attr('src','../../res/img/common/sx.png');
                    $('.screen').removeClass('common');
                    $(".all").html('现货<img src="../../res/img/common/downJt.png" />');
                    $('#usedList').empty();//清空容器
                    page = 0;
                    totalSize = 0;
                    maintypes = '';
                    minmoney = '1';
                    maxmoney = '999999';
                    sptypes = '0,1,3,4,5,6,7,8';
                    datatype = '2';
                    loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,datatype,key)
                }
            },
            {
                text: '预售',
                onClick: function () {
                    $('.option').removeClass('check');
                    $('#screenImg').attr('src','../../res/img/common/sx.png');
                    $('.screen').removeClass('common');
                    $(".all").html('预售<img src="../../res/img/common/downJt.png" />');
                    $('#usedList').empty();//清空容器
                    page = 0;
                    sptypes = '2';
                    maintypes = '';
                    minmoney = '1';
                    maxmoney = '999999';
                    datatype = '3';
                    loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,datatype,key)
                }
            },
            // {
            //     text: '银币商品',
            //     onClick: function () {
            //         $('.option').removeClass('check');
            //         $('#screenImg').attr('src','../../res/img/common/sx.png');
            //         $('.screen').removeClass('common');
            //         $(".all").html('银币商品<img src="../../res/img/common/downJt.png" />');
            //         $('#usedList').empty();//清空容器
            //         mRefresh.resetUpLoading(true);
            //         page = 0;
            //         sptypes = '5';
            //         maintypes = '';
            //         loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
            //     }
            // },
            {
                text: '返回',
                onClick: function () {

                }
            }];
        $.modal({
            title: '请选择品类',
            verticalButtons: true,
            buttons:buttons1
        });
    });

    $('.priceSel').click(function () {
        $('#usedList').empty();//清空容器
        // mRefresh.resetUpLoading(true);
        var thisHtml = $(this).find('span').html();
        if(thisHtml === '价格升序'){
            sort = 'money10';
            page = -1;
            $('.newsBest').removeClass('common');
            $(this).find('span').addClass('common').html('价格降序');
            loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,datatype,key)
        }else {
            sort = 'money01';
            page = -1;
            $('.newsBest').removeClass('common');
            $(this).find('span').addClass('common').html('价格升序');
            loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,datatype,key)

        }
    });

    $('.newsBest').click(function () {
        $('#usedList').empty();//清空容器
        // mRefresh.resetUpLoading(true);
        sort = 'time';
        $('.selectCom span').removeClass('common');
        $(this).addClass('common');
        page = -1;
        loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,datatype,key)
    });

    //筛选模块
    $('.screen').click(function () {
        $('.screenIndex').show().removeClass('bounceOutRight').addClass('bounceInRight');
        $(".indexAlert").css("height",$(".alertContent").height())
    });
    $('.commodityType .option').click(function () {
        if($(this).hasClass('check')){
            $(this).removeClass('check');
        }else {
            $(this).addClass('check');
        }
    });

    $('.commodityAttribute .option').click(function () {
        if($(this).hasClass('check')){
            $(this).removeClass('check');
        }else {
            $(this).addClass('check');
        }
    });

    $('.commodityEncryption .option').click(function () {
        if($(this).hasClass('check')){
            $(this).removeClass('check');
            filter = 1
        }else {
            $(this).addClass('check');
            filter = 0
        }
    });

    $('.confirm').click(function () {
        $(".all").html('全部<img src="../../res/img/common/downJt.png" />');
        page = 0;
        totalSize = 0;
        var minMoney = $('#minmoney').val();
        var maxMoney = $('#maxmoney').val();
        if(Number(minMoney) > Number(maxMoney)){
            $.toast('最低价不能大于最高价');
            return
        }

        if(maxMoney > '999999'){
            $.toast('最高价不能大于999999');
            return
        }
        var commodityTypeArr = $(".commodityType .check");
        var commodityTypeArrPush = [];
        var commodityTypeArrPushs = [];
        commodityTypeArr.each(function(){
            commodityTypeArrPush.push($(this).attr("optioncode"));
            commodityTypeArrPushs.push($(this).attr("optioncodes"));
        });
        var commodityTypeStr = commodityTypeArrPush.toString(',');
        var commodityTypeStrs = commodityTypeArrPushs.toString(',');
        var commodityAttributeArr = $(".commodityAttribute .check");
        var commodityAttributeArrPush = [];
        commodityAttributeArr.each(function(){
            commodityAttributeArrPush.push($(this).attr("optionCode"));
        });
        var commodityAttributeStr = commodityAttributeArrPush.toString(',');
        if(commodityTypeStr === '' && commodityAttributeStr === '' && minMoney === '' && maxMoney === ''){
            $('#screenImg').attr('src','../../res/img/common/sx.png');
            $('.screen').removeClass('common');
        }else {
            $('.screen').addClass('common');
            $('#screenImg').attr('src','../../res/img/common/sxSec.png');
        }
        if(minMoney === ''){
            minmoney = '1'
        }else {
            minmoney = minMoney
        }

        if(maxMoney === ''){
            maxmoney = '99999'
        }else {
            maxmoney =maxMoney
        }
        $('#usedList').empty();//清空容器
        sptypes = commodityTypeStr;
        datatype = commodityTypeStrs;
        maintypes = commodityAttributeStr;
        loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,datatype,key);
        $('.screenIndex').removeClass('bounceInRight').addClass('bounceOutRight');
    });

    $('.reset').click(function () {
        $('.option').removeClass('check');
        $('#usedList').empty();//清空容器;
        $('#screenImg').attr('src','../../res/img/common/sx.png');
        $('.screen').removeClass('common');
        sptypes = '';
        maintypes = '';
        totalSize = 0;
        page = 0;
        minmoney = '1';
        maxmoney = '999999';
        datatype = '2,3';
        loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,datatype,key);
    });

    $('.indexAlert').click(function () {
        $('.screenIndex').removeClass('bounceInRight').addClass('bounceOutRight');
    });

    var mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#usedList').empty();
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,datatype,key)
            }
        }
    });

    function loadpagedata (minmoney,maxmoney,page,sptypes,maintypes,sort,datatype,key) {
        var data = {
            'markid':markid,
            'is_app':'0',
            'minmoney':minmoney,
            'maxmoney':maxmoney,
            'page':page,
            'sptypes':sptypes,
            'maintypes':maintypes,
            'rowcount':30,
            'djsort':sort,
            'datatype':datatype,
            'key':key
        };
        getData('post','/niu/apiv3/niu_getlisthpv5.php',data,function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                var append = '';
                if(data.pagetotal === 0){
                    mRefresh.endUpLoading(true);
                    $('.minirefresh-hardware-speedup').hide();
                    $('.nullContent').show();
                    return
                }else {
                    $('.nullContent').hide();
                    $('.minirefresh-hardware-speedup').show();
                }
                $(data.data).each(function () {
                    let syNum = Number(this.money2) + Number(this.extra_money2)
                    append += '<li class="HPItem">' +
                        '                   <input type="hidden" class="id" value="'+this.id+'"> '+
                        '                    <div class="pic">' +
                        '                        <img src="'+this.picbanner+'" alt="">' +
                        '                    </div>' +
                        '                    <div class="name">'+this.name+'</div>' +
                        '                    <div class="price1">' +
                        '                        <img class="goldCoin" src="../../res/img/common/goldCoin.png" alt="">' +
                        '                        <span class=goldCoinNum>'+syNum+'</span>' +
                        '                    </div>' +
                        // '                    <div class="info1">可用'+this.money2+'金币代付</div>' +
                        '                </li>'

                });
                $('#usedList').append(append);
                if (page === 0){
                    mRefresh.scrollTo('5','30');
                }


                totalSize = totalSize + 10;
                if (totalSize > data.datacount){
                    mRefresh.endUpLoading(true);
                } else{
                    mRefresh.resetUpLoading(true);
                    mRefresh.endUpLoading(false);
                }
            }else {
                $.toast(data.msg)
            }
        })
    }

    $("#usedList li").live("click", function(){
        let id = $(this).find('input').val();
        window.location.href = 'dancool://fullweb='+urlView+'HPStore/HPStoreDetail.html?poolid='+id+'&markid='+markid
    });
});
